home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / NFind / Sources / NewReplaceTool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-18  |  39.8 KB  |  991 lines  |  [TEXT/MPS ]

  1. /* NewReplaceTool.c
  2.  *
  3.  * MPW tool to provide an enhanced front end to the standard MPW shell replace command
  4.  */
  5.  
  6. #include <OSUtils.h>
  7. #include <Strings.h>
  8. #include <Signal.h>
  9. #include <StdLib.h>
  10. #include <Scrap.h>
  11. #include <ToolUtils.h>
  12. #include <Lists.h>
  13. #include <Types.h>
  14. #include <Dialogs.h>
  15. #include <TextEdit.h>
  16. #include <Events.h>
  17. #include <OSEvents.h>
  18. #include <Fonts.h>
  19. #include <Quickdraw.h>
  20. #include <StdIo.h>
  21. #include <String.h>
  22.  
  23. pascal char InputFilter(DialogPtr pdlogReplace,EventRecord *pevntReplace,short *psHit);
  24. void RefreshDialog(DialogPtr pdlogReplace);
  25. void QuoteString(char *pszOutString,char *pszInString);
  26. void ReplaceDialog(void);
  27. void main(void);
  28.  
  29. #define REPLACE_BUTTON                1
  30. #define CANCEL_BUTTON                2
  31. #define REPLACE_ALL_BUTTON            3
  32. #define FIND_BUTTON                    4
  33. #define CASE_SENSITIVE_CHECK        5
  34. #define SEARCH_BACKWARD_CHECK        6
  35. #define LITERAL_RADIO                7
  36. #define ENTIRE_WORD_RADIO            8
  37. #define SELECTION_EXPRESSION_RADIO    9
  38. #define FIND_TEXT_EDIT                10
  39. #define REPLACE_TEXT_EDIT             11
  40.  
  41. char chDialogExit;
  42. char chDoubleClick;
  43. Point ptAction;
  44. ListHandle hlistFind;
  45. Rect rectFindList;
  46. ListHandle hlistReplace;
  47. Rect rectReplaceList;
  48. short sFontNum;
  49.  
  50. pascal char InputFilter(DialogPtr pdlogReplace,EventRecord *pevntReplace,short *psHit)
  51. /* dialog input filter */
  52.  
  53. {
  54. Rect rectTemp;
  55. char chRetVal,chTest;
  56. short sDialogType,sLen;
  57. Handle hDialogItem;
  58. int iKeyCode,iCharCode;
  59. Boolean fCmdDown;
  60. Cell cellTemp;
  61. Str255 strTemp;
  62.  
  63. chRetVal = false;                                                                                    /* initialize */
  64. if (pevntReplace->what == mouseDown)
  65.     {
  66.     /* there was a mouse down event */
  67.     ptAction = pevntReplace->where;                                                                 /* get click position */
  68.     GlobalToLocal(&ptAction);                                                                        /* convert to local coordinates */
  69.     if (PtInRect(ptAction,&rectFindList) == true)
  70.         {
  71.         /* an event occured in the find text list */
  72.         chDoubleClick = LClick(ptAction,pevntReplace->modifiers,hlistFind);
  73.         if (chDoubleClick == true)
  74.             {
  75.             cellTemp = LLastClick(hlistFind);                                                        /* find where the click occured */
  76.             sLen = 255;                                                                                /* maximum data length */
  77.             LGetCell((strTemp + 1),&sLen,cellTemp,hlistFind);                                        /* get a copy of the clicked on text */
  78.             strTemp[0] = (unsigned char) sLen;                                                        /* set the length byte */
  79.             GetDItem(pdlogReplace,FIND_TEXT_EDIT,&sDialogType,&hDialogItem,&rectTemp);                /* get the item handle */
  80.             SetIText(hDialogItem,strTemp);                                                            /* set the new text string */
  81.             SelIText(pdlogReplace,FIND_TEXT_EDIT,sLen,sLen);                                        /* set the new insertion point */
  82.             *psHit = FIND_TEXT_EDIT;                                                                /* signal item affected */
  83.             chRetVal = true;                                                                        /* signal processing complete */
  84.             }  /* if chDoubleClick */
  85.         }  /* if PtInRect() */
  86.     if (PtInRect(ptAction,&rectReplaceList) == true)
  87.         {
  88.         /* an event occured in the replace text list */
  89.         chDoubleClick = LClick(ptAction,pevntReplace->modifiers,hlistReplace);
  90.         if (chDoubleClick == true)
  91.             {
  92.             cellTemp = LLastClick(hlistReplace);                                                    /* find where the click occured */
  93.             sLen = 255;                                                                                /* maximum data length */
  94.             LGetCell((strTemp + 1),&sLen,cellTemp,hlistReplace);                                    /* get a copy of the clicked on text */
  95.             strTemp[0] = (unsigned char) sLen;                                                        /* set the length byte */
  96.             GetDItem(pdlogReplace,REPLACE_TEXT_EDIT,&sDialogType,&hDialogItem,&rectTemp);            /* get the item handle */
  97.             SetIText(hDialogItem,strTemp);                                                            /* set the new text string */
  98.             SelIText(pdlogReplace,REPLACE_TEXT_EDIT,sLen,sLen);                                        /* set the new insertion point */
  99.             *psHit = REPLACE_TEXT_EDIT;                                                             /* signal item affected */
  100.             chRetVal = true;                                                                        /* signal processing complete */
  101.             }  /* if chDoubleClick */
  102.         }  /* if PtInRect() */
  103.     }  /* if pevntReplace */
  104. else
  105.     {
  106.     if (pevntReplace->what == keyDown)
  107.         {
  108.         /* there was a keypress event */
  109.         iKeyCode = pevntReplace->message & keyCodeMask;                                                /* get the key code */
  110.         iKeyCode = iKeyCode >> 8;                                                                     /* shift to low order byte */
  111.         if ((iKeyCode == 0x24) || (iKeyCode == 0x4c))
  112.             {
  113.             /* Return or Enter were pressed - treat as exit request */
  114.             chRetVal = true;                                                                        /* signal processing complete */
  115.             *psHit = ok;                                                                            /* signal item affected */
  116.             }  /* if iKeyCode */
  117.         else
  118.             {
  119.             fCmdDown = ((pevntReplace->modifiers & cmdKey) != 0);                                    /* is the Command key down? */
  120.             if (fCmdDown)
  121.                 {
  122.                 *psHit = ((DialogPeek) pdlogReplace)->editField + 1;                                 /* signal item effected (current TE field) */
  123.                 iCharCode = pevntReplace->message & charCodeMask;                                    /* get the character code */
  124.                 chTest = (char) iCharCode;                                                            /* convert it to a character */
  125.                 switch (chTest)
  126.                     {
  127.                     case 'x' :
  128.                         {
  129.                         DlgCut(pdlogReplace);                                                        /* perform cut to scrap */
  130.                         chRetVal = true;                                                            /* signal processing complete */
  131.                         break;
  132.                         }  /* case 'x' */
  133.                     case 'c' :
  134.                         {
  135.                         DlgCopy(pdlogReplace);                                                         /* perform copy to scrap */
  136.                         chRetVal = true;                                                            /* signal processing complete */
  137.                         break;
  138.                         }  /* case 'c' */
  139.                     case 'v' :
  140.                         {
  141.                         DlgPaste(pdlogReplace);                                                        /* perform paste from scrap */
  142.                         chRetVal = true;                                                            /* signal processing complete */
  143.                         break;
  144.                         }  /* case 'v' */
  145.                     case '.' :
  146.                         {
  147.                         chRetVal = true;                                                            /* signal processing complete */
  148.                         *psHit = cancel;                                                            /* signal item affected */
  149.                         break;
  150.                         }  /* case '.' */
  151.                     }  /* switch chTest */
  152.                 }  /* if fCmdDown */
  153.             }  /* else if iKeyCode */
  154.         }  /* if pevntReplace */
  155.     else
  156.         {
  157.         if (pevntReplace->what == updateEvt)
  158.             {
  159.             if (IsDialogEvent(pevntReplace))
  160.                 {
  161.                 RefreshDialog(pdlogReplace);                                                        /* update the non-controls */
  162.                 }  /* if IsDialogEvent() */
  163.             }  /* if pevntReplace */
  164.         }  /* else if pevntReplace */
  165.     }  /* else if pevntReplace */
  166. return chRetVal;                                                                                    /* signal if processing is complete */
  167. }  /* InputFilter() */
  168.  
  169. void RefreshDialog(DialogPtr pdlogReplace)
  170. /* update non-controls in the dialog */ 
  171.  
  172. {
  173. Rect rectTemp;
  174. short sDialogType,sTemp;
  175. Handle hDialogItem;
  176. ControlHandle hctrlItem;
  177.  
  178. GetDItem(pdlogReplace,REPLACE_BUTTON,&sDialogType,&hDialogItem,&rectTemp);                        /* get the item handle */
  179. PenSize(3,3);                                                                                    /* change pen to draw thick default outline */
  180. InsetRect(&rectTemp,-4,-4);                                                                     /* draw outside the button by 1 pixel */
  181. FrameRoundRect(&rectTemp,16,16);                                                                /* draw the outline */
  182. PenSize(1,1);                                                                                    /* restore the pen size to the default value */
  183.  
  184. /* Draw the upper divide line */
  185. PenPat(qd.gray);                                                                                /* set this to a gray line */
  186. MoveTo(15,204);                                                                                 /* move to starting position */
  187. LineTo(585,204);                                                                                /* draw to ending position */
  188.  
  189. /* Draw the lower divide line */
  190. MoveTo(15,280);                                                                                 /* move to starting position */
  191. LineTo(585,280);                                                                                /* draw to ending position */
  192. PenPat(qd.black);                                                                                /* revert to default pen pattern */
  193.  
  194. /* Draw the find prompt text */
  195. TextFont(systemFont);
  196. TextSize(12);
  197. GetDItem(pdlogReplace,SELECTION_EXPRESSION_RADIO,&sDialogType,&hDialogItem,&rectTemp);            /* get the item handle */
  198. hctrlItem = (ControlHandle) hDialogItem;                                                        /* change dialog handle to control handle */
  199. sTemp = GetCtlValue(hctrlItem);                                                                    /*    get the current Checkbox value */
  200. SetRect(&rectTemp,15,153,290,169);
  201. EraseRect(&rectTemp);
  202. MoveTo(15,166);
  203. if (sTemp)
  204.     {
  205.     DrawString("\pFind what selection expression?");
  206.     }  /* if sTemp */
  207. else
  208.     {
  209.     DrawString("\pFind what string?");
  210.     }  /* else if sTemp */
  211.     
  212. /* Draw the replace prompt text */
  213. SetRect(&rectTemp,310,153,585,169);
  214. EraseRect(&rectTemp);
  215. MoveTo(310,166);
  216. DrawString("\pReplace with what string?");
  217. TextFont(sFontNum);
  218. TextSize(9);
  219.  
  220. LUpdate(pdlogReplace->visRgn,hlistFind);                                                            /* update this list */
  221. rectTemp = rectFindList;                                                                            /* start with full size */
  222. InsetRect(&rectTemp,-1,-1);                                                                         /* set for framing */
  223. FrameRect(&rectTemp);                                                                                /* frame it */
  224.  
  225. LUpdate(pdlogReplace->visRgn,hlistReplace);                                                         /* update this list */
  226. rectTemp = rectReplaceList;                                                                            /* start with full size */
  227. InsetRect(&rectTemp,-1,-1);                                                                         /* set for framing */
  228. FrameRect(&rectTemp);                                                                                /* frame it */
  229. }  /* RefreshDialog() */
  230.  
  231. void AddListString(Str255 *strAdd,ListHandle hlstString)
  232. /* add strings to an existing list */
  233.  
  234. {
  235. short sRow;
  236. Point ptSize;
  237.  
  238. if (hlstString != nil) 
  239.     {
  240.     ptSize.h = 0;                                            /* point to the correct column */
  241.     sRow = LAddRow(1,32000,hlstString);                        /* add another row at the end of the list */
  242.     ptSize.v = sRow;                                        /* point to the row just added */
  243.     LSetCell((*strAdd + 1),*strAdd[0],ptSize,hlstString);     /* place string in row just created */
  244.     LDraw(ptSize,hlstString);                                /* draw the new string */
  245.     }  /* if hlstString */
  246. }  /* AddListString() */
  247.  
  248. void QuoteString(char *pszOutString,char *pszInString)
  249. /* quote ' characters in the input string */
  250.  
  251. {
  252. char *pchEnd,*pchLast,*pchStart;
  253.  
  254. *pszOutString = '\0';                                                            /* initialize to empty */
  255. pchStart = pszInString;                                                            /* initialize */
  256. pchEnd = pszInString;                                                            /* initialize */
  257. pchLast = pchStart + strlen(pszInString);
  258. while (pchEnd != NULL)
  259.     {
  260.     pchEnd = strchr(pchStart,(int) '\'');                                        /* find the next occurance of the character ', if any */
  261.     if ((pchEnd != NULL) && (pchStart <= pchLast))
  262.         {
  263.         /* a ' character was found */
  264.         (void) strncat(pszOutString,pchStart,(size_t) (pchEnd - pchStart));        /* concatenate the input string up to but not including the ' character */
  265.         (void) strcat(pszOutString,"'∂''");                                        /* quote the ' character as ∂' */
  266.         pchStart = pchEnd + 1;                                                    /* update the string start position */
  267.         }  /* if pchEnd */
  268.     else
  269.         {
  270.         /* no more ' characters to be found */
  271.         (void) strcat(pszOutString,pchStart);                                    /* concatenate the remaining input string */
  272.         }  /* else if pchEnd */
  273.     }  /* while pchEnd */
  274. }  /* QuoteString() */
  275.  
  276. void ReplaceDialog(void)
  277. /* Find and Replace dialog box routine */
  278.  
  279. {
  280. DialogPtr pdlogReplace;
  281. Rect rectData,rectTemp;
  282. short sDialogType,sIndex,sHit,sSearchType,sTemp;
  283. Boolean fBeep,fGoOn,fMatch,fReplace,fReplaceAll,fReverse,fSearchBackward;
  284. Handle hdlogItem;
  285. ControlHandle hctrlItem,hcrtlTemp;
  286. Str255 strTemp1,strTemp2;
  287. Point ptCSize;
  288. TEHandle hteString;
  289. DialogPeek pkdlogReplace;
  290. char *pszEnv;
  291. char achTemp[256],achQuote1[256],achQuote2[256];
  292. size_t usLen;
  293. FILE *pfilFindCommand,*pfilReplaceCommand,*pfilSetup;
  294. FontInfo fntiPrompt;
  295. KeyMap kmapExit;
  296.  
  297. pdlogReplace = GetNewDialog(2,nil,(WindowPtr) -1);                                                                    /* bring in the dialog resource */
  298. pszEnv = getenv("font");
  299. (void) strcpy(achTemp,pszEnv);
  300. c2pstr(achTemp);
  301. GetFNum(achTemp,&sFontNum);
  302.  
  303. rectTemp.top = pdlogReplace->portRect.top;                                                                            /* get window size, we will now center it */
  304. rectTemp.left = pdlogReplace->portRect.left;                                                                        /* ditto */
  305. rectTemp.bottom = pdlogReplace->portRect.bottom;                                                                    /* ditto */
  306. rectTemp.right = pdlogReplace->portRect.right;                                                                        /* ditto */
  307. rectTemp.top = -pdlogReplace->portBits.bounds.top;                                                                    /* fixed vertical position */
  308. rectTemp.left = ((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - (rectTemp.right - rectTemp.left)) / 2;    /* center horizontally */
  309. MoveWindow(pdlogReplace,rectTemp.left,rectTemp.top,true);                                                            /* move the window to the proper position */
  310. SetPort(pdlogReplace);                                                                                                /* prepare to add conditional text */
  311.  
  312. TextFont(sFontNum);    
  313. TextSize(9);
  314. GetFontInfo(&fntiPrompt);
  315. pkdlogReplace = (DialogPeek) pdlogReplace;                                                                            /* get to the inner record */
  316. hteString = pkdlogReplace->textH;                                                                                     /* get to the TE record */
  317. HLock((Handle) hteString);                                                                                            /* lock it for safety */
  318. (*hteString)->txFont = sFontNum;
  319. (*hteString)->txSize = 9;
  320. (*hteString)->fontAscent = fntiPrompt.ascent;
  321. (*hteString)->lineHeight = fntiPrompt.ascent + fntiPrompt.descent + fntiPrompt.leading;
  322. HUnlock((Handle) hteString);                                                                                         /* unLock the handle when done */
  323.  
  324. SetRect(&rectFindList,15,15,290,147);
  325. rectTemp = rectFindList;                                                                                            /* start with full size */
  326. rectTemp.right = rectTemp.right - 15;                                                                                /* make room for the scroll bar on the right */
  327. if (rectTemp.right <= (rectTemp.left + 15))
  328.     {
  329.     /* Safety check */
  330.     rectTemp.right = rectTemp.left + 15;
  331.     }  /* if rectTemp */
  332. InsetRect(&rectTemp,-1,-1);                                                                                         /* set for framing */
  333. FrameRect(&rectTemp);                                                                                                /* frame it */
  334. InsetRect(&rectTemp,1,1);                                                                                            /* restore */
  335. SetRect(&rectData,0,0,1,0);                                                                                            /* set up list */
  336. ptCSize.h = rectTemp.right - rectTemp.left;                                                                            /* width of the list */
  337. ptCSize.v = 0;                                                                                                        /* let List Manager determine the height */
  338. hlistFind = LNew(&rectTemp,&rectData,ptCSize,0,pdlogReplace,true,false,false,true);                                    /* make the list */
  339. HLock((Handle) hlistFind);
  340. (*hlistFind)->selFlags = lOnlyOne + lNoNilHilite;                                                                    /* set the attributes */
  341. HUnlock((Handle) hlistFind);
  342. LDoDraw(true,hlistFind);                                                                                            /* draw the list */
  343.  
  344. pszEnv = getenv("findstrings");                                                                                        /* get a pointer to the FindStrings environment variable */
  345. if (pszEnv != NULL)
  346.     {
  347.     pfilSetup = fopen(pszEnv,"r");                                                                                    /* try to open the file for reading */
  348.     if (pfilSetup != NULL)
  349.         {
  350.         sIndex = 0;
  351.         while (!feof(pfilSetup))
  352.             {
  353.             if (fgets(achTemp,256,pfilSetup) != NULL)
  354.                 {
  355.                 usLen = strlen(achTemp);
  356.                 if (usLen > 0)
  357.                     {
  358.                     if (*(achTemp + usLen - 1) == (char) '\n')
  359.                         {
  360.                         usLen--;
  361.                         *(achTemp + usLen) = '\0';                                                                    /* remove the line feed character */
  362.                         }  /* if *achTemp */
  363.                     c2pstr(achTemp);
  364.                     AddListString((Str255 *)achTemp,hlistFind);
  365.                     sIndex++;
  366.                     }  /* if usLen */
  367.                 }  /* if fgets() */
  368.             }  /* while !feof() */
  369.         (void) fclose(pfilSetup);
  370.         if (sIndex > 0)
  371.             {
  372.             ptCSize.v = sIndex - 1;                                                                                    /* cell row of last item */
  373.             ptCSize.h = 0;                                                                                            /* set cell column */
  374.             LSetSelect(true,ptCSize,hlistFind);                                                                        /* select the cell */
  375.             LAutoScroll(hlistFind);                                                                                    /* scroll as necessary to display the selected item */
  376.             }  /* if sIndex */
  377.         }  /* if pfilSetup */
  378.     }  /* if pszEnv */
  379.  
  380. SetRect(&rectReplaceList,310,15,585,147);
  381. rectTemp = rectReplaceList;                                                                                            /* start with full size */
  382. rectTemp.right = rectTemp.right - 15;                                                                                /* make room for the scroll bar on the right */
  383. if (rectTemp.right <= (rectTemp.left + 15))
  384.     {
  385.     /* safety check */
  386.     rectTemp.right = rectTemp.left + 15;
  387.     }  /* if rectTemp */
  388. InsetRect(&rectTemp,-1,-1);                                                                                         /* set for framing */
  389. FrameRect(&rectTemp);                                                                                                /* frame it */
  390. InsetRect(&rectTemp,1,1);                                                                                            /* restore */
  391. SetRect(&rectData,0,0,1,0);                                                                                            /* set list up */
  392. ptCSize.h = rectTemp.right - rectTemp.left;                                                                            /* width of the list */
  393. ptCSize.v = 0;                                                                                                        /* let List Manager determine the height */
  394. hlistReplace = LNew(&rectTemp,&rectData,ptCSize,0,pdlogReplace,true,false,false,true);                                 /* make the list */
  395. HLock((Handle) hlistReplace);
  396. (*hlistReplace)->selFlags = lOnlyOne + lNoNilHilite;                                                                /* set the attributes */
  397. HUnlock((Handle) hlistReplace);
  398. LDoDraw(true,hlistReplace);                                                                                            /* draw the list */
  399.  
  400. pszEnv = getenv("replacestrings");                                                                                    /* get a pointer to the FindStrings environment variable */
  401. if (pszEnv != NULL)
  402.     {
  403.     pfilSetup = fopen(pszEnv,"r");                                                                                    /* try to open the file for reading */
  404.     if (pfilSetup != NULL)
  405.         {
  406.         sIndex = 0;
  407.         while (!feof(pfilSetup))
  408.             {
  409.             if (fgets(achTemp,256,pfilSetup) != NULL)
  410.                 {
  411.                 usLen = strlen(achTemp);
  412.                 if (usLen > 0)
  413.                     {
  414.                     if (*(achTemp + usLen - 1) == (char) '\n')
  415.                         {
  416.                         usLen--;
  417.                         *(achTemp + usLen) = '\0';                                                                    /* remove the line feed character */
  418.                         }  /* if *achTemp */
  419.                     c2pstr(achTemp);
  420.                     AddListString((Str255 *)achTemp,hlistReplace);
  421.                     sIndex++;
  422.                     }  /* if usLen */
  423.                 }  /* if fgets() */
  424.             }  /* while !feof() */
  425.         (void) fclose(pfilSetup);
  426.         if (sIndex > 0)
  427.             {
  428.             ptCSize.v = sIndex - 1;                                                                                    /* cell row of last item */
  429.             ptCSize.h = 0;                                                                                            /* set cell column */
  430.             LSetSelect(true,ptCSize,hlistReplace);                                                                    /* select the cell */
  431.             LAutoScroll(hlistReplace);                                                                                /* scroll as necessary to display the selected item */
  432.             }  /* if sIndex */
  433.         }  /* if pfilSetup */
  434.     }  /* if pszEnv */
  435.  
  436. /* set up check boxes according to the enviroment variables */
  437. GetDItem(pdlogReplace,CASE_SENSITIVE_CHECK,&sDialogType,&hdlogItem,&rectTemp);                                        /* get the item handle */
  438. hcrtlTemp = (ControlHandle) hdlogItem;                                                                                /* change dialog handle to control handle */
  439. pszEnv = getenv("casesensitive");                                                                                    /* get a pointer to the CaseSensitive environment variable */
  440. if (atoi(pszEnv) == 0)
  441.     {
  442.     SetCtlValue(hcrtlTemp,0);                                                                                        /* turn the check box off */
  443.     }  /* if atoi() */
  444. else
  445.     {
  446.     SetCtlValue(hcrtlTemp,1);                                                                                        /* turn the check box on */
  447.     }  /* else if atoi() */
  448. GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp);                                        /* get the item handle */
  449. hcrtlTemp = (ControlHandle) hdlogItem;                                                                                /* change dialog handle to control handle */
  450. pszEnv = getenv("searchbackward");                                                                                    /* get a pointer to the SearchBackward environment variable */
  451. if (atoi(pszEnv) == 0)
  452.     {
  453.     SetCtlValue(hcrtlTemp,0);                                                                                        /* turn the check box off */
  454.     }  /* if atoi() */
  455. else
  456.     {
  457.     SetCtlValue(hcrtlTemp,1);                                                                                        /* turn the check box on */
  458.     }  /* else if atoi() */
  459.  
  460. /* set up radio buttons according to the enviroment variable */
  461. for (sIndex = LITERAL_RADIO; sIndex <= SELECTION_EXPRESSION_RADIO; sIndex++)
  462.     {
  463.     /* clear all radios */
  464.     GetDItem(pdlogReplace,sIndex,&sDialogType,&hdlogItem,&rectTemp);                                                /* get the Radio handle */
  465.     hcrtlTemp = (ControlHandle) hdlogItem;                                                                            /* get the control handle */
  466.     SetCtlValue(hcrtlTemp,0);                                                                                        /* turn the radio selection off */
  467.     }  /* for sIndex */
  468. pszEnv = getenv("searchtype");                                                                                        /* get a pointer to the SearchType environment variable */
  469. sIndex = LITERAL_RADIO + atoi(pszEnv);                                                                                /* get the default radio sIndex */
  470. GetDItem(pdlogReplace,sIndex,&sDialogType,&hdlogItem,&rectTemp);                                                    /* get the item handle */
  471. hctrlItem = (ControlHandle) hdlogItem;                                                                                /* change dialog handle to control handle */
  472. SetCtlValue(hctrlItem,1);                                                                                            /* select the radio */
  473.  
  474. /* set up initial highlighting */
  475. if (sIndex == SELECTION_EXPRESSION_RADIO)
  476.     {
  477.     GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp);                                    /* get the item handle */
  478.     hcrtlTemp = (ControlHandle) hdlogItem;                                                                            /* change dialog handle to control handle */
  479.     HiliteControl(hcrtlTemp,255);                                                                                    /* set to disabled */
  480.     }  /* if sIndex */
  481.  
  482. /* set up the default replacement string according to the enviroment variables */
  483. GetDItem(pdlogReplace,REPLACE_TEXT_EDIT,&sDialogType,&hdlogItem,&rectTemp);                                            /* get the item handle */
  484. pszEnv = getenv("lastreplacestring");                                                                                /* get a pointer to the LastReplaceString environment variable */
  485. if (pszEnv != NULL)
  486.     {
  487.     pfilSetup = fopen(pszEnv,"r");                                                                                    /* try to open the file for reading */
  488.     if (pfilSetup != NULL)
  489.         {
  490.         if (fgets(achTemp,256,pfilSetup) != NULL)
  491.             {
  492.             usLen = strlen(achTemp);
  493.             if (usLen > 0)
  494.                 {
  495.                 if (*(achTemp + usLen - 1) == (char) '\n')
  496.                     {
  497.                     usLen--;
  498.                     *(achTemp + usLen) = '\0';                                                                        /* remove the line feed character */
  499.                     }  /* if *achTemp */
  500.                 }  /* if usLen */
  501.             }  /* if fgets() */
  502.         (void) fclose(pfilSetup);
  503.         }  /* if pfilSetup */
  504.     c2pstr(achTemp);
  505.     SetIText(hdlogItem,achTemp);
  506.     SelIText(pdlogReplace,REPLACE_TEXT_EDIT,0,(short) strlen(pszEnv));                                                /* set the new insertion point */
  507.     }  /* if pszEnv */
  508. else
  509.     {
  510.     SetIText(hdlogItem,"\p");
  511.     SelIText(pdlogReplace,REPLACE_TEXT_EDIT,0,0);                                                                    /* set the new insertion point */
  512.     }  /* else if pszEnv */
  513.     
  514. /* set up the default find string/selection expression according to the enviroment variables */
  515. GetDItem(pdlogReplace,FIND_TEXT_EDIT,&sDialogType,&hdlogItem,&rectTemp);                                            /* get the item handle */
  516. pszEnv = getenv("lastfindstring");                                                                                    /* get a pointer to the LastFindString environment variable */
  517. if (pszEnv != NULL)
  518.     {
  519.     pfilSetup = fopen(pszEnv,"r");                                                                                    /* try to open the file for reading */
  520.     if (pfilSetup != NULL)
  521.         {
  522.         if (fgets(achTemp,256,pfilSetup) != NULL)
  523.             {
  524.             usLen = strlen(achTemp);
  525.             if (usLen > 0)
  526.                 {
  527.                 if (*(achTemp + usLen - 1) == (char) '\n')
  528.                     {
  529.                     usLen--;
  530.                     *(achTemp + usLen) = '\0';                                                                        /* remove the line feed character */
  531.                     }  /* if *achTemp */
  532.                 }  /* if usLen */
  533.             }  /* if fgets() */
  534.         (void) fclose(pfilSetup);
  535.         }  /* if pfilSetup */
  536.     c2pstr(achTemp);
  537.     SetIText(hdlogItem,achTemp);
  538.     SelIText(pdlogReplace,FIND_TEXT_EDIT,0,(short) strlen(pszEnv));                                                    /* set the new insertion point */
  539.     }  /* if pszEnv */
  540. else
  541.     {
  542.     SetIText(hdlogItem,"\p");
  543.     SelIText(pdlogReplace,FIND_TEXT_EDIT,0,0);                                                                        /* set the new insertion point */
  544.     }  /* else if pszEnv */
  545.  
  546. RefreshDialog(pdlogReplace);                                                                                        /* draw the non-controls */
  547. ShowWindow(pdlogReplace);                                                                                            /* open a dialog box */
  548. SelectWindow(pdlogReplace);                                                                                         /* make it visible */
  549.  
  550. chDialogExit = false;                                                                                                 /* do not exit dialog handle loop yet */
  551. do
  552.     {
  553.     ModalDialog((ModalFilterProcPtr) InputFilter,&sHit);                                                             /* wait until an item is hit */
  554.     GetDItem(pdlogReplace,sHit,&sDialogType,&hdlogItem,&rectTemp);                                                     /* get item information */
  555.     hctrlItem = (ControlHandle) hdlogItem;                                                                            /* get the control handle */
  556.     
  557.     if (sHit == REPLACE_BUTTON)
  558.         {
  559.         chDialogExit = true;                                                                                        /* exit the dialog when this selection is made */
  560.         }  /* if sHit */
  561.     
  562.     if (sHit == CANCEL_BUTTON)
  563.         {
  564.         chDialogExit = true;                                                                                        /* exit the dialog when this selection is made */
  565.         }  /* if sHit */
  566.     
  567.     if (sHit == REPLACE_ALL_BUTTON)
  568.         {
  569.         chDialogExit = true;                                                                                        /* exit the dialog when this selection is made */
  570.         }  /* if sHit */
  571.     
  572.     if (sHit == FIND_BUTTON)
  573.         {
  574.         chDialogExit = true;                                                                                        /* exit the dialog when this selection is made */
  575.         }  /* if sHit */
  576.     
  577.     if (sHit == CASE_SENSITIVE_CHECK)
  578.         {
  579.         sTemp = GetCtlValue(hctrlItem);                                                                                /*    get the current Checkbox value */
  580.         SetCtlValue(hctrlItem,((sTemp + 1) & 1));                                                                    /* toggle the value */
  581.         }  /* if sHit */
  582.     
  583.     if (sHit == SEARCH_BACKWARD_CHECK)
  584.         {
  585.         sTemp = GetCtlValue(hctrlItem);                                                                                /*    get the current Checkbox value */
  586.         SetCtlValue(hctrlItem,((sTemp + 1) & 1));                                                                    /* toggle the value */
  587.         }  /* if sHit */
  588.     
  589.     if ((sHit >= LITERAL_RADIO) && (sHit <= SELECTION_EXPRESSION_RADIO))
  590.         {
  591.         if (sHit == SELECTION_EXPRESSION_RADIO)
  592.             {
  593.             sTemp = GetCtlValue(hctrlItem);                                                                            /*    get the current Checkbox value */
  594.             if (!(sTemp & 1))
  595.                 {
  596.                 /* disable SEARCH_BACKWARD_CHECK */
  597.                 GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp);                        /* get the item handle */
  598.                 hcrtlTemp = (ControlHandle) hdlogItem;                                                                /* change dialog handle to control handle */
  599.                 HiliteControl(hcrtlTemp,255);                                                                        /* set to disabled */
  600.  
  601.                 /* update the find prompt text */
  602.                 SetRect(&rectTemp,15,153,290,169);
  603.                 EraseRect(&rectTemp);
  604.                 TextFont(systemFont);
  605.                 TextSize(12);
  606.                 MoveTo(15,166);
  607.                 DrawString("\pFind what selection expression?");
  608.                 TextFont(sFontNum);
  609.                 TextSize(9);
  610.                 }  /* else if !sTemp */
  611.             }  /* if sHit */
  612.         else
  613.             {
  614.             GetDItem(pdlogReplace,SELECTION_EXPRESSION_RADIO,&sDialogType,&hdlogItem,&rectTemp);                    /* get the item handle */
  615.             hcrtlTemp = (ControlHandle) hdlogItem;                                                                    /* change dialog handle to control handle */
  616.             sTemp = GetCtlValue(hcrtlTemp);                                                                            /*    get the current Checkbox value */
  617.             if (sTemp & 1)
  618.                 {
  619.                 /* enable SEARCH_BACKWARD_CHECK */
  620.                 GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp);                        /* get the item handle */
  621.                 hcrtlTemp = (ControlHandle) hdlogItem;                                                                /* change dialog handle to control handle */
  622.                 HiliteControl(hcrtlTemp,0);                                                                            /* set to no highlighting */
  623.  
  624.                 /* update the find prompt text */
  625.                 SetRect(&rectTemp,15,153,290,169);
  626.                 EraseRect(&rectTemp);
  627.                 TextFont(systemFont);
  628.                 TextSize(12);
  629.                 MoveTo(15,166);
  630.                 DrawString("\pFind what string?");
  631.                 TextFont(sFontNum);
  632.                 TextSize(9);
  633.                 }  /* if sTemp */
  634.             }  /* else if sHit */
  635.         for (sIndex = LITERAL_RADIO; sIndex <= SELECTION_EXPRESSION_RADIO; sIndex++)
  636.             {
  637.             /* clear all radios */
  638.             GetDItem(pdlogReplace,sIndex,&sDialogType,&hdlogItem,&rectTemp);                                        /* get the Radio handle */
  639.             hcrtlTemp = (ControlHandle) hdlogItem;                                                                    /* get the control handle */
  640.             SetCtlValue(hcrtlTemp,0);                                                                                /* turn the radio selection off */
  641.             }  /* for sIndex */
  642.         SetCtlValue(hctrlItem,1);                                                                                    /* turn the one radio selection on */
  643.         }  /* if sHit */
  644.     }  /* do */
  645. while (chDialogExit == false);
  646.  
  647. /* Get results after dialog */
  648. fGoOn = true;                                                                                                        /* initialize */
  649. fReplace = true;                                                                                                    /* initialize */
  650. fReplaceAll = false;                                                                                                /* initialize */
  651. GetKeys(kmapExit);
  652. if ((kmapExit[1] & 1) > 0)
  653.     {
  654.     /* a shift key was held down when exiting the dialog - sTemporarily reverse the search direction */
  655.     fReverse = true;
  656.     }  /* if kmapExit[] */
  657. else
  658.     {
  659.     fReverse = false;
  660.     }  /* else if kmapExit[] */
  661. switch (sHit)
  662.     {
  663.     case REPLACE_BUTTON :
  664.         {
  665.         /* do nothing */
  666.         break;
  667.         }  /* case REPLACE_BUTTON */
  668.     case CANCEL_BUTTON :
  669.         {
  670.         fGoOn = false;
  671.         break;
  672.         }  /* case CANCEL_BUTTON */
  673.     case REPLACE_ALL_BUTTON :
  674.         {
  675.         fReplaceAll = true;
  676.         break;
  677.         }  /* case REPLACE_ALL_BUTTON */
  678.     case FIND_BUTTON :
  679.         {
  680.         fReplace = false;
  681.         break;
  682.         }  /* case FIND_BUTTON */
  683.     }  /* switch sHit */
  684.  
  685. if (fGoOn)
  686.     {
  687.     fBeep = true;                                                                                                    /* initialize */
  688.     GetDItem(pdlogReplace,FIND_TEXT_EDIT,&sDialogType,&hdlogItem,&rectTemp);                                        /* get the item handle */
  689.     GetIText(hdlogItem,&strTemp1);                                                                                     /* get the text entered */
  690.     if ((int) strTemp1[0] > 0)
  691.         {
  692.         pfilReplaceCommand = fopen(getenv("replaceagainscript"),"w");
  693.         if (pfilReplaceCommand != NULL)
  694.             {
  695.             pfilFindCommand = fopen(getenv("findagainscript"),"w");
  696.             if (pfilFindCommand != NULL)
  697.                 {
  698.                 /* the command output files were successfully opened */
  699.                 fBeep = false;                                                                                        /* disable error beep */
  700.                 GetDItem(pdlogReplace,CASE_SENSITIVE_CHECK,&sDialogType,&hdlogItem,&rectTemp);                        /* get the Checkbox handle */
  701.                 hctrlItem = (ControlHandle) hdlogItem;                                                                /* get the Checkbox handle */
  702.                 (void) fprintf(pfilReplaceCommand,"Set CaseSensitive %d\n",GetCtlValue(hctrlItem));
  703.                     
  704.                 GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp);                        /* get the Checkbox handle */
  705.                 hctrlItem = (ControlHandle) hdlogItem;                                                                /* get the Checkbox handle */
  706.                 fSearchBackward = (Boolean) GetCtlValue(hctrlItem);
  707.                 (void) fprintf(pfilReplaceCommand,"Set SearchBackward %u\n",fSearchBackward);
  708.                 
  709.                 sIndex = LITERAL_RADIO;                                                                                /* start at the first radio in this group */
  710.                 do
  711.                     {
  712.                     GetDItem(pdlogReplace,sIndex,&sDialogType,&hdlogItem,&rectTemp);                                /* get the radio handle */
  713.                     hctrlItem = (ControlHandle) hdlogItem;                                                            /* get the radio handle */
  714.                     sTemp = GetCtlValue(hctrlItem);                                                                    /* get the radio value */
  715.                     sIndex++;
  716.                     }  /* do */
  717.                 while ((sTemp != 1) && (sIndex <= SELECTION_EXPRESSION_RADIO));
  718.                 sSearchType = sIndex - LITERAL_RADIO - 1;
  719.                 (void) fprintf(pfilReplaceCommand,"Set SearchType %d\n",sSearchType);
  720.                 
  721.                 p2cstr(strTemp1);
  722.                 pszEnv = getenv("lastfindstring");                                                                    /* get a pointer to the LastFindString environment variable */
  723.                 if (pszEnv != NULL)
  724.                     {
  725.                     pfilSetup = fopen(pszEnv,"w");                                                                    /* try to open the file for writing */
  726.                     if (pfilSetup != NULL)
  727.                         {
  728.                         (void) fprintf(pfilSetup,"%s\n",strTemp1);
  729.                         (void) fclose(pfilSetup);
  730.                         }  /* if pfilSetup */
  731.                     }  /* if pszEnv */
  732.         
  733.                     /* add the new find string to the saved find string file, but only if it is unique */
  734.                     pszEnv = getenv("findstrings");                                                                    /* get a pointer to the FindStrings environment variable */
  735.                     if (pszEnv != NULL)
  736.                         {
  737.                         pfilSetup = fopen(pszEnv,"r+");                                                                /* try to open the file for reading */
  738.                         if (pfilSetup != NULL)
  739.                             {
  740.                             fMatch = false;                                                                            /* initialize */
  741.                             while (!feof(pfilSetup) && !fMatch)
  742.                                 {
  743.                                 if (fgets(achTemp,256,pfilSetup) != NULL)
  744.                                     {
  745.                                     usLen = strlen(achTemp);
  746.                                     if (usLen > 0)
  747.                                         {
  748.                                         if (*(achTemp + usLen - 1) == (char) '\n')
  749.                                             {
  750.                                             usLen--;
  751.                                             *(achTemp + usLen) = '\0';                                                /* remove the line feed character */
  752.                                             }  /* if *achTemp */
  753.                                         fMatch = fMatch || (strcmp(achTemp,strTemp1) == 0);
  754.                                         }  /* if usLen */
  755.                                     }  /* if fgets() */
  756.                                 }  /* while !feof() */
  757.                             if (!fMatch)
  758.                                 {
  759.                                 (void) fprintf(pfilSetup,"%s\n",strTemp1);                                            /* add the unique new string to the end of the file */
  760.                                 }  /* if !fMatch */
  761.                             (void) fclose(pfilSetup);
  762.                             }  /* if pfilSetup */
  763.                         }  /* if pszEnv */
  764.                     
  765.                 if (fReverse)
  766.                     {
  767.                     fSearchBackward = !fSearchBackward;
  768.                     }  /* if fReverse */
  769.                     
  770.                 if (fReplace)
  771.                     {
  772.                     GetDItem(pdlogReplace,REPLACE_TEXT_EDIT,&sDialogType,&hdlogItem,&rectTemp);                        /* get the item handle */
  773.                     GetIText(hdlogItem,&strTemp2);                                                                     /* get the text entered */
  774.                     p2cstr(strTemp2);
  775.                     pszEnv = getenv("lastreplacestring");                                                            /* get a pointer to the LastFindString environment variable */
  776.                     if (pszEnv != NULL)
  777.                         {
  778.                         pfilSetup = fopen(pszEnv,"w");                                                                /* try to open the file for writing */
  779.                         if (pfilSetup != NULL)
  780.                             {
  781.                             (void) fprintf(pfilSetup,"%s\n",strTemp2);
  782.                             (void) fclose(pfilSetup);
  783.                             }  /* if pfilSetup */
  784.                         }  /* if pszEnv */
  785.                     
  786.         
  787.                     /* add the new replace string to the saved replace string file, but only if it is unique */
  788.                     pszEnv = getenv("replacestrings");                                                                /* get a pointer to the FindStrings environment variable */
  789.                     if (pszEnv != NULL)
  790.                         {
  791.                         pfilSetup = fopen(pszEnv,"r+");                                                                /* try to open the file for reading */
  792.                         if (pfilSetup != NULL)
  793.                             {
  794.                             fMatch = false;                                                                            /* initialize */
  795.                             while (!feof(pfilSetup) && !fMatch)
  796.                                 {
  797.                                 if (fgets(achTemp,256,pfilSetup) != NULL)
  798.                                     {
  799.                                     usLen = strlen(achTemp);
  800.                                     if (usLen > 0)
  801.                                         {
  802.                                         if (*(achTemp + usLen - 1) == (char) '\n')
  803.                                             {
  804.                                             usLen--;
  805.                                             *(achTemp + usLen) = '\0';                                                /* remove the line feed character */
  806.                                             }  /* if *achTemp */
  807.                                         fMatch = fMatch || (strcmp(achTemp,strTemp2) == 0);
  808.                                         }  /* if usLen */
  809.                                     }  /* if fgets() */
  810.                                 }  /* while !feof() */
  811.                             if (!fMatch)
  812.                                 {
  813.                                 (void) fprintf(pfilSetup,"%s\n",strTemp2);                                            /* add the unique new string to the end of the file */
  814.                                 }  /* if !fMatch */
  815.                             (void) fclose(pfilSetup);
  816.                             }  /* if pfilSetup */
  817.                         }  /* if pszEnv */
  818.                         
  819.                     switch (sSearchType)
  820.                         {
  821.                         case 0 : /* literal */
  822.                             {
  823.                             QuoteString(achQuote1,strTemp1);                                                        /* quote ' characters as necessary */
  824.                             QuoteString(achQuote2,strTemp2);                                                        /* quote ' characters as necessary */
  825.                             if (fReplaceAll)
  826.                                 {
  827.                                 if (fSearchBackward)
  828.                                     {
  829.                                     (void) fprintf(pfilReplaceCommand,"Replace -c ∞ \\'%s'\\ '%s' \"{Active}\"\n",achQuote1,achQuote2);
  830.                                     (void) fprintf(pfilFindCommand,"Find \\'%s'\\ \"{Active}\"\n",achQuote1);
  831.                                     }  /* if fSearchBackward */
  832.                                 else
  833.                                     {
  834.                                     (void) fprintf(pfilReplaceCommand,"Replace -c ∞ /'%s'/ '%s' \"{Active}\"\n",achQuote1,achQuote2);
  835.                                     (void) fprintf(pfilFindCommand,"Find /'%s'/ \"{Active}\"\n",achQuote1);
  836.                                     }  /* else if fSearchBackward */
  837.                                 }  /* if fReplaceAll */
  838.                             else
  839.                                 {
  840.                                 if (fSearchBackward)
  841.                                     {
  842.                                     (void) fprintf(pfilReplaceCommand,"Replace \\'%s'\\ '%s' \"{Active}\"\n",achQuote1,achQuote2);
  843.                                     (void) fprintf(pfilFindCommand,"Find \\'%s'\\ \"{Active}\"\n",achQuote1);
  844.                                     }  /* if fSearchBackward */
  845.                                 else
  846.                                     {
  847.                                     (void) fprintf(pfilReplaceCommand,"Replace /'%s'/ '%s' \"{Active}\"\n",achQuote1,achQuote2);
  848.                                     (void) fprintf(pfilFindCommand,"Find /'%s'/ \"{Active}\"\n",achQuote1);
  849.                                     }  /* else if fSearchBackward */
  850.                                 }  /* else if fReplaceAll */
  851.                             (void) fprintf(pfilReplaceCommand,"if {Status} != 0\nBeep\nend\n");
  852.                             (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nEnd\n");
  853.                             break;
  854.                             }  /* case 0 */
  855.                         case 1 : /* entire word */
  856.                             {
  857.                             QuoteString(achQuote1,strTemp1);                                                        /* quote ' characters as necessary */
  858.                             QuoteString(achQuote2,strTemp2);                                                        /* quote ' characters as necessary */
  859.                             if (fReplaceAll)
  860.                                 {
  861.                                 if (fSearchBackward)
  862.                                     {
  863.                                     (void) fprintf(pfilReplaceCommand,"Replace -c ∞ \\([¬{WordSet}])®1'%s'([¬{WordSet}])®2\\ '®1''%s''®2' \"{Active}\"\n",achQuote1,achQuote2);
  864.                                     (void) fprintf(pfilFindCommand,"Mark -y § '_New_Find_' \"{Active}\"\n");
  865.                                     (void) fprintf(pfilFindCommand,"Find §¡1 \"{Active}\"\nFind Δ\\[¬{WordSet}]'%s'[¬{WordSet}]\\!1:/'%s'/Δ \"{Active}\"\nIf ({Status} != 0)\nFind '_New_Find_' \"{Active}\"\nBeep\nEnd\n",achQuote1,achQuote1);
  866.                                     (void) fprintf(pfilFindCommand,"Unmark '_New_Find_' \"{Active}\"\n");
  867.                                     }  /* if fSearchBackward */
  868.                                 else
  869.                                     {
  870.                                     (void) fprintf(pfilReplaceCommand,"Replace -c ∞ /([¬{WordSet}])®1'%s'([¬{WordSet}])®2/ '®1''%s''®2' \"{Active}\"\n",achQuote1,achQuote2);
  871.                                     (void) fprintf(pfilFindCommand,"Find Δ/[¬{WordSet}]'%s'[¬{WordSet}]/ \"{Active}\"\n",achQuote1);
  872.                                     (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nElse\nFind /'%s'/ \"{Active}\"\nEnd\n",achQuote1);
  873.                                     }  /* else if fSearchBackward */
  874.                                 }  /* if fReplaceAll */
  875.                             else
  876.                                 {
  877.                                 if (fSearchBackward)
  878.                                     {
  879.                                     (void) fprintf(pfilReplaceCommand,"Replace \\([¬{WordSet}])®1'%s'([¬{WordSet}])®2\\ '®1''%s''®2' \"{Active}\"\n",achQuote1,achQuote2);
  880.                                     (void) fprintf(pfilFindCommand,"Mark -y § '_New_Find_' \"{Active}\"\n");
  881.                                     (void) fprintf(pfilFindCommand,"Find §¡1 \"{Active}\"\nFind Δ\\[¬{WordSet}]'%s'[¬{WordSet}]\\!1:/'%s'/Δ \"{Active}\"\nIf ({Status} != 0)\nFind '_New_Find_' \"{Active}\"\nBeep\nEnd\n",achQuote1,achQuote1);
  882.                                     (void) fprintf(pfilFindCommand,"Unmark '_New_Find_' \"{Active}\"\n");
  883.                                     }  /* if fSearchBackward */
  884.                                 else
  885.                                     {
  886.                                     (void) fprintf(pfilReplaceCommand,"Replace /([¬{WordSet}])®1'%s'([¬{WordSet}])®2/ '®1''%s''®2' \"{Active}\"\n",achQuote1,achQuote2);
  887.                                     (void) fprintf(pfilFindCommand,"Find Δ/[¬{WordSet}]'%s'[¬{WordSet}]/ \"{Active}\"\n",achQuote1);
  888.                                     (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nElse\nFind /'%s'/ \"{Active}\"\nEnd\n",achQuote1);
  889.                                     }  /* else if fSearchBackward */
  890.                                 }  /* else if fReplaceAll */
  891.                             (void) fprintf(pfilReplaceCommand,"if {Status} != 0\nBeep\nend\n");
  892.                             break;
  893.                             }  /* case 1 */
  894.                         case 2 : /* selection expression */
  895.                             {
  896.                             if (fReplaceAll)
  897.                                 {
  898.                                 (void) fprintf(pfilReplaceCommand,"Replace -c ∞ %s '%s' \"{Active}\"\n",strTemp1,strTemp2);
  899.                                 }  /* if fReplaceAll */
  900.                             else
  901.                                 {
  902.                                 (void) fprintf(pfilReplaceCommand,"Replace %s '%s' \"{Active}\"\n",strTemp1,strTemp2);
  903.                                 }  /* else if fReplaceAll */
  904.                             (void) fprintf(pfilReplaceCommand,"if {Status} != 0\nBeep\nend\n");
  905.                             (void) fprintf(pfilFindCommand,"Find %s \"{Active}\"\n",strTemp1);
  906.                             (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nEnd\n");
  907.                             break;
  908.                             }  /* case 2 */
  909.                         }  /* switch sSearchType */
  910.                     }  /* if fReplace */
  911.                 else
  912.                     {
  913.                     switch (sSearchType)
  914.                         {
  915.                         case 0 : /* literal */
  916.                             {
  917.                             QuoteString(achQuote1,strTemp1);                                                        /* quote ' characters as necessary */
  918.                             if (fSearchBackward)
  919.                                 {
  920.                                 (void) fprintf(pfilReplaceCommand,"Find \\'%s'\\ \"{Active}\"\n",achQuote1);
  921.                                 (void) fprintf(pfilFindCommand,"Find \\'%s'\\ \"{Active}\"\n",achQuote1);
  922.                                 }  /* if fSearchBackward */
  923.                             else
  924.                                 {
  925.                                 (void) fprintf(pfilReplaceCommand,"Find /'%s'/ \"{Active}\"\n",achQuote1);
  926.                                 (void) fprintf(pfilFindCommand,"Find /'%s'/ \"{Active}\"\n",achQuote1);
  927.                                 }  /* else if fSearchBackward */
  928.                             (void) fprintf(pfilReplaceCommand,"If {Status} != 0\nBeep\nEnd\n");
  929.                             (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nEnd\n");
  930.                             break;
  931.                             }  /* case 0 */
  932.                         case 1 : /* entire word */
  933.                             {
  934.                             QuoteString(achQuote1,strTemp1);                                                        /* quote ' characters as necessary */
  935.                             if (fSearchBackward)
  936.                                 {
  937.                                 (void) fprintf(pfilReplaceCommand,"Mark -y § '_New_Find_' \"{Active}\"\n");
  938.                                 (void) fprintf(pfilReplaceCommand,"Find §¡1 \"{Active}\"\nFind Δ\\[¬{WordSet}]'%s'[¬{WordSet}]\\!1:/'%s'/Δ \"{Active}\"\nIf ({Status} != 0)\nFind '_New_Find_' \"{Active}\"\nBeep\nEnd\n",achQuote1,achQuote1);
  939.                                 (void) fprintf(pfilReplaceCommand,"Unmark '_New_Find_' \"{Active}\"\n");
  940.                                 (void) fprintf(pfilFindCommand,"Mark -y § '_New_Find_' \"{Active}\"\n");
  941.                                 (void) fprintf(pfilFindCommand,"Find §¡1 \"{Active}\"\nFind Δ\\[¬{WordSet}]'%s'[¬{WordSet}]\\!1:/'%s'/Δ \"{Active}\"\nIf ({Status} != 0)\nFind '_New_Find_' \"{Active}\"\nBeep\nEnd\n",achQuote1,achQuote1);
  942.                                 (void) fprintf(pfilFindCommand,"Unmark '_New_Find_' \"{Active}\"\n");
  943.                                 }  /* if fSearchBackward */
  944.                             else
  945.                                 {
  946.                                 (void) fprintf(pfilReplaceCommand,"Find Δ/[¬{WordSet}]'%s'[¬{WordSet}]/ \"{Active}\"\n",achQuote1);
  947.                                 (void) fprintf(pfilReplaceCommand,"If {Status} != 0\nBeep\nElse\nFind /'%s'/ \"{Active}\"\nEnd\n",achQuote1);
  948.                                 (void) fprintf(pfilFindCommand,"Find Δ/[¬{WordSet}]'%s'[¬{WordSet}]/ \"{Active}\"\n",achQuote1);
  949.                                 (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nElse\nFind /'%s'/ \"{Active}\"\nEnd\n",achQuote1);
  950.                                 }  /* else if fSearchBackward */
  951.                             break;
  952.                             }  /* case 1 */
  953.                         case 2 : /* selection expression */
  954.                             {
  955.                             (void) fprintf(pfilReplaceCommand,"Find %s \"{Active}\"\n",strTemp1);
  956.                             (void) fprintf(pfilReplaceCommand,"If {Status} != 0\nBeep\nEnd\n");
  957.                             (void) fprintf(pfilFindCommand,"Find %s \"{Active}\"\n",strTemp1);
  958.                             (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nEnd\n");
  959.                             break;
  960.                             }  /* case 2 */
  961.                         }  /* switch sSearchType */
  962.                     }  /* else if fReplace */
  963.                 (void) fclose(pfilFindCommand);
  964.                 }  /* if pfilFindCommand */
  965.             (void) fclose(pfilReplaceCommand);
  966.             }  /* if pfilReplaceCommand */
  967.         }  /* if strTemp1[] */
  968.     if (fBeep)
  969.         {
  970.         SysBeep(3);                                                                                                    /* signal error */
  971.         }  /* if fBeep */
  972.     }  /* if fGoOn */
  973. LDispose(hlistFind);
  974. LDispose(hlistReplace);
  975. DisposDialog(pdlogReplace);                                                                                         /* flush the dialog out of memory */
  976. }  /* ReplaceDialog() */
  977.  
  978. void main(void)
  979. {
  980. (void) signal(SIGINT,SIG_IGN);        /* disable command-. because aborting with the dialog up is a BAD THING */
  981. InitGraf(&qd.thePort);                /* initialize QuickDraw */
  982. SetFScaleDisable(true);
  983. FlushEvents(everyEvent,0);            /* start with no pending events to handle */
  984. InitCursor();                        /* start with a visible arrow cursor */
  985. (void) TEFromScrap();                /* Get any MPW text scrap */
  986. ReplaceDialog();                    /* open the modal dialog */
  987. FlushEvents(everyEvent,0);            /* exit with no pending events to handle */
  988. exit(0);
  989. }  /* main() */
  990.  
  991. /* end of NewReplaceTool.c */